
# ======  后台管理模型 ======

from django.db import models

# 会员模型
class Users(models.Model):
    # 用户名
    username = models.CharField(max_length = 32)
    # 邮箱
    email = models.CharField(max_length = 50,null=True)
    # 手机号
    phone = models.CharField(max_length = 11)
    # 密码
    password = models.CharField(max_length = 80)
    # 年龄
    age = models.IntegerField(null=True)
    # 性别
    sex = models.CharField(max_length=1,default='0')
    # 头像
    pic = models.CharField(max_length=200,default='1.jpg')
    # 状态 0 正常  1禁用
    isDelete = models.BooleanField(default=False)
    # 注册时间
    addtime = models.DateTimeField(auto_now_add=True)


    # 给自定义的模型,设置自定义的权限
    class Meta:
        permissions = (
            ("show_users", "查看会员权限"),
            ("insert_users", "添加会员权限"),
            ("edit_users", "编辑会员权限"),
            ("del_users", "删除会员权限"),
        )

# 商品分类模型
class Types(models.Model):
    # 分类名称
    name = models.CharField(max_length=10)
    # 分类id
    pid = models.IntegerField()
    # 分类路径
    path = models.CharField(max_length=20)

      # 自定义权限
    class Meta:
        permissions = (
            ("show_types", "查看商品分类权限"),
            ("insert_types", "添加商品分类权限"),
            ("edit_types", "编辑商品分类权限"),
            ("del_types", "删除商品分类权限"),
        )


# 商品模型
class Goods(models.Model):
    # 外键
    tid = models.ForeignKey(to="Types", to_field="id")
    # 标题
    title = models.CharField(max_length=125)
    # 图片
    price = models.DecimalField(max_digits=10, decimal_places=2)
    # 库存
    store = models.IntegerField()
    # 详情
    info = models.TextField()
    # 详情图片
    pic = models.CharField(max_length=100)
    # 点击数
    clicknum = models.IntegerField(default=0)
    # 订单数
    ordernum = models.IntegerField(default=0)
    # 状态
    # 1 新发布  2 热卖  0缺货
    status = models.IntegerField(default=1)
    # 是否下架
    isDelete = models.BooleanField(default=False)
    # 新增时间
    addtime = models.DateTimeField(auto_now_add=True)

    # 自定义权限
    class Meta:
        permissions = (
            ("show_goods", "查看商品权限"),
            ("insert_goods", "添加商品权限"),
            ("edit_goods", "编辑商品权限"),
            ("del_goods", "删除商品权限"),
        )

# 购物车模型
class Cart(models.Model):
    uid = models.ForeignKey(to='Users',to_field='id')
    gid = models.ForeignKey(to='Goods',to_field='id')
    num = models.IntegerField()
    addtime = models.DateTimeField(auto_now_add=True)

# 收货地址模型
class Address(models.Model):
    uid = models.ForeignKey(to='Users',to_field='id')
    name = models.CharField(max_length=10)
    phone = models.CharField(max_length=11)
    address = models.CharField(max_length=50)
    isSelect =  models.BooleanField(default=False)


# 订单模型
class Order(models.Model):
    uid = models.ForeignKey(to='Users',to_field='id')
    addressid = models.ForeignKey(to='Address',to_field='id')
    totalprice = models.FloatField()
    # 0已取消 1 未支付 2 已支付,待发货  3 已发货,待收货  4 以收货 待评价
    status = models.IntegerField()
    addtime = models.DateTimeField(auto_now_add=True)

     # 自定义权限
    class Meta:
        permissions = (
            ("show_order", "查看订单权限"),
            ("edit_order", "编辑订单权限"),
        )

# 订单详情
class OrderInfo(models.Model):
    orderid = models.ForeignKey(to='Order',to_field='id')
    goodsid = models.ForeignKey(to='Goods',to_field='id')
    num = models.IntegerField()
    price = models.FloatField()